/*eslint-disable */

import Vue from 'vue';
import Vuex from 'vuex';
import _ from "lodash";
import {
    scaleOrdinal
} from 'd3-scale';

const debug = process.env.NODE_ENV !== 'production';

Vue.use(Vuex);

export default new Vuex.Store({
    strict: debug,
    state: {
        ST_location_id_2_name: {
            1: "Palace Hills",
            2: "Northwest",
            3: "Old Town",
            4: "Safe Town",
            5: "Southwest",
            6: "Downtown",
            7: "Wilson Forest",
            8: "Scenic Vista",
            9: "Broadview",
            10: "Chapparal",
            11: "Terrapin Springs",
            12: "Pepper Mill",
            13: "Cheddarford",
            14: "Easton",
            15: "Weston",
            16: 'Southton',
            17: "Oak Willow",
            18: "East Parton",
            19: "West Parton"
        },
        ST_location_id_2_color: {
            1: "#bc6236",
            2: "#2b736f",
            3: "#65082F",
            4: "#7e706c",
            5: "#35838d",
            6: "#94c8d8",
            7: "#a31e21",
            8: "#dfb19a",
            9: "#ea5e51",
            10: "#643f5a",
            11: "#b91e45",
            12: "#9b4980",
            13: "#0997fd",
            14: "#512e47",
            15: "#c5b1db",
            16: '#643e46',
            17: "#e99a9a",
            18: "#045f6b",
            19: "#8ca581"
        },
        ST_location_id_2_coord: {
            1: [-13353294, 19069],
            2: [-13349093, 19991],
            3: [-13340993, 21991],
            4: [-13335993, 18991],
            5: [-13350364, 13207],
            6: [-13348560, 15207],
            7: [-13327281, 9053],
            8: [-13329281, 4553],
            9: [-13339454, 3919],
            10: [-13334203, 7016],
            11: [-13330861, 6809],
            12: [-13330258,
                10925],
            13: [-13335258,
                10925],
            14: [-13342958,
                17225],
            15: [-13345958,
                17225],
            16: [-13345958,
                14225],
            17: [-13339958,
                8925],
            18: [-13339258,
                12925],
            19: [-13343258,
                12925]

        },
       
        // 测试计划列表
        planList: [],
        starterId: 1,
        // range: [0, 250000],
        selection: [], // 选中结果
        selected: false, // 是否进行了选择
        brushed_types: [],
        brushed_dates: [],
        highlight_testids: [], //目前来看是只在`原始数据分析`里有用的
        testids_for_timeline: [],
        color: scaleOrdinal()
            .domain(['start_type0', 'start_type1', 'abnormal'])
            .range(['#57d2f7', '#726dd1', '#ff0000']),
        testId: '',
        channelColor: {
            speed: '#1f77b4',
            u30: '#ff7f0e',
            u45: '#2ca02c',
            u50: '#d62728',
            i30: '#9467bd',
            i50: '#8c564b'
        },
        trend_colors: ["#0a9650", "#c33b37"],
        abnormal_type2color: {
            0: '#c7e9b5',
            1: '#a1d8f5', //蓝色
            2: '#fa9fb5', //#3acab6
            3: '#dd3497', //
            4: '#49006a', //紫色
            5: '#7a0177', //
            6: '#fde0dd', //
            '-1': '#A1000e', //
            "A1": "#fde0dd",
            "A2": "#fa9fb5",
            "A3": "#dd3497",
            "A4": "#7a0177",
            "A5": "#49006a",
            "A6": "#893448",
            "A7": "#d95850",
            "A8": "#eb8146",
            "A9": "#ffb248",
            "A10": "#f2d643",
            "A11": "#ebdba4",
            "C1": "#c7e9b5",
            "C2": "#a1d8f5",
            "U1": "#A1000e"
        },
        abnormal_type2count: {

        },
        feature_channel_2_max: {
            "speed": 1500,
            "u30": 30,
            "u45": 30,
            "u50": 30,
            "i30": 1000,
            "i50": 20,
            "polohousing": 100,
            "busbar": 200,
            "solenoidswitch": 100,
            "oil": 100,
            "water": 200
        },
        gray_colors: ["#DCDCDC", "#808080", "#c7c7c7"],
        channel2yAxisName: {
            "speed": "Engine Speed (rmp)",
            "u30": "U30 (V)",
            "u45": "U45 (V)",
            "u50": "U50 (V)",
            "i30": "I30 (A)",
            "i50": "I50 (A)",
            "polohousing": "POLOHOUSING (℃)",
            "oil": "OIL (℃)",
            "solenoidswitch": "SOLENOIDSWITCH (℃)",
            "water": "WATER (℃)",
            "busbar": "BUSBAR (℃)"
        }

    },
    getters: {
        ST_location_id_2_name(state) {
            return state.ST_location_id_2_name;
        },
        ST_location_id_2_color(state) {
            return state.ST_location_id_2_color;
        },
        ST_location_id_2_coord(state) {
            return state.ST_location_id_2_coord;
        },
        /**
         * @returns {String}
         * @description 当前数据集的名字
         */
        current_dataset_name(state) {
            let dataset = state.planList.find(e => e['key'] == state.starterId);
            return dataset ? dataset['label'] : ""
        },
        abnormal_type2comment(state) {
            let obj = {
                "-1": "U1"
            };
            let sorted_all = _.filter(_.mapValues(state.abnormal_type2count, (count, type) => [type, count]), e => parseInt(e) >= 0).sort((a, b) => b[1] - a[1]);
            let C1, C2;
            if (sorted_all[0]) {
                C1 = sorted_all[0][0];
                obj[C1] = 'C1'
            }
            if (sorted_all[1]) {
                C2 = sorted_all[1][0];
                obj[C2] = "C2";
            }
            if (sorted_all.length > 2) {
                for (let i = 2; i < sorted_all.length; i++) {
                    obj[sorted_all[i][0]] = `A${i - 1}`
                }
            }
            // debugger



            // let sortedAns = _.filter(_.mapValues(state.abnormal_type2count, (count, type) => [type, count]), a => parseInt(a) > 1).sort((a, b) => b[1] - a[1])
            // for (let i = 0; i < sortedAns.length; i++) {
            //     obj[sortedAns[i][0]] = `A${i+1}`
            // }
            return obj;
        },
        axis_font_size: () => {
            return 11;
        }
    },
    mutations: {
        // planlist初始化
        initPlanList(state, payload) {
            state.planList = payload;
        },
        // 改变测试计划id
        changeStarter(state, id) {
            state.starterId = id;
        },
        // 单选开始操作
        hasSelect(state, bool) {
            state.selected = bool;
        },
        // 设置列表点击的testId
        setTestId(state, id) {
            state.testId = id;
        },
        // 更新列表数据
        setTestList(state, selection) {
            state.selection = selection;
        },
        changeTestSet(state, testSet) {
            state.test_set = testSet;
        },
        brushedDatesAndTypes(state, datesAndTypes) {
            state.brushed_dates = datesAndTypes['dates'];
            state.brushed_types = datesAndTypes['types'];
        },
        highlight_testids(state, testids) {
            state.highlight_testids = testids;
        },
        /**
         * @param {{String:Number}} type_counts
         */
        type2counts(state, type_counts) {
            state.abnormal_type2count = type_counts;
        },
        /**
         *
         * @param {NUmber[]} testids
         */
        testids4Timeline(state, testids) {
            state.testids_for_timeline = testids;
        }
    },
    actions: {}
});
